window.PartnerHelper = {
    init(param, cb) {
        wx.showShareMenu({ withShareTicket: true });
        wx.onShareAppMessage(() => {
            // 用户点击了“转发”按钮
            return {
                title: '转发标题'
            }
        })

        wx.onShow((res) => {
            G.partner.onShow(res);
        })
        wx.onHide((res) => {
            G.partner.onHide(res);
        })

    },

    share(param) {
        wx.shareAppMessage({
            title: param.title,
            // imageUrl: param.imageUrl || canvas.toTempFilePathSync({
            //     x: 10,
            //     y: 10,
            //     width: 200,
            //     height: 150,
            //     destWidth: 400,
            //     destHeight: 300
            // })
        })
    },

    _getIsFitHeight() {
        if (!this.sysInfo) {
            this.sysInfo = wx.getSystemInfoSync();
        }
        let isFitHeight = false;
        let designScale = G.config.canvasWidth / G.config.canvasHeight;
        if (designScale <= this.sysInfo.screenWidth / this.sysInfo.screenHeight) {
            isFitHeight = true;
        }
        return isFitHeight;
    },

    _getLocalPix(num) {
        if (!this.sysInfo) {
            this.sysInfo = wx.getSystemInfoSync();
        }
        let isFitHeight = this._getIsFitHeight();
        let scale;
        if (isFitHeight) {
            scale = G.config.canvasHeight / (this.sysInfo.screenHeight * this.sysInfo.pixelRatio);
        } else {
            scale = G.config.canvasWidth / (this.sysInfo.screenWidth * this.sysInfo.pixelRatio);
        }

        return num / (scale * this.sysInfo.pixelRatio);
    },

    _getLocalX(x) {
        if (!this.sysInfo) {
            this.sysInfo = wx.getSystemInfoSync();
        }

        let localOffsetX = this._getLocalPix(x);
        return this.sysInfo.screenWidth / 2 + localOffsetX;
    },
    _getLocalY(y) {
        if (!this.sysInfo) {
            this.sysInfo = wx.getSystemInfoSync();
        }

        let localOffsetY = this._getLocalPix(y);
        return this.sysInfo.screenHeight / 2 - localOffsetY;
    },

    createGameClub() {
        if (this.btnGameClub) {
            this.showGameClub();
            return;
        }
        let x = 213;
        let y = -260;
        let width = 106;
        let height = 119;
        this.btnGameClub = wx.createGameClubButton({
            icon: '',
            style: {
                left: this._getLocalX(x - width / 2),
                top: this._getLocalY(y + height / 2),
                width: this._getLocalPix(width),
                height: this._getLocalPix(height)
            }
        })
    },
    showGameClub() {
        if (!this.btnGameClub) {
            return;
        }
        this.btnGameClub.show();
    },
    hideGameClub() {
        if (!this.btnGameClub) {
            return;
        }
        this.btnGameClub.hide();
    },

    postMessage(k, v) {
        let openDataContext = wx.getOpenDataContext()
        openDataContext.postMessage({
            key: k,
            param: v
        })
    },

    setUserCloudStorage(k, v, cb) {
        wx.setUserCloudStorage({
            KVDataList: [{ key: parseStr(k), value: parseStr(v) }],
            success: function () {
                cb && cb();
            },
            fail: function () {
                cb && cb(true);
            }
        });
    },

    vibrateShort() {
        wx.vibrateShort();
    },

    vibrateLong() {
        wx.vibrateLong();
    },

    navigateToMiniProgram(appid) {
        wx.navigateToMiniProgram({
            appId: appid,
            path: '',
            success(res) {
                // 打开成功
            }
        })
    },






    ///////////////ad
    initAd(param) {
        ADMgr.init();
    },
    show(adType, cb) {
        ADMgr.show(adType, cb);
    },
    hide(adType) {
        ADMgr.hide(adType);
    },
}
'9a2f25c2c3ebd53895f4289e494d214f'
const BannerUnitId = 'adunit-660e6164eec77a02';
const VideoUnitId = 'adunit-efd9ac035a8e531a';
const InterstitialUnitId = 'adunit-8dcfe07730d9df0d';
const ADMgr = {
    AD_BANNER: "BANNER",
    AD_INTERSTITIAL: "INTERSTITIAL",
    AD_VIDEO: "VIDEO",
    EVENT_SHOW_SUCC: "EVENT_SHOW_SUCC",
    EVENT_SHOW_FAIL: "EVENT_SHOW_FAIL",
    // 初始化广告组件
    init() {
        this.initBannerAd();
        this.initVideoAd();
        this.initInterstitialAd();
    },

    initBannerAd() {
        // banner广告上方对应游戏的y坐标
        let bannerAdHeight = -360;
        if (!PartnerHelper.sysInfo) {
            PartnerHelper.sysInfo = wx.getSystemInfoSync();
        }
        var tw = PartnerHelper.sysInfo.screenWidth;
        var th = PartnerHelper.sysInfo.screenHeight;
        var tY = PartnerHelper._getLocalY(bannerAdHeight); // 广告最上方的位置（微信坐标系下）
        var tHeight = th / 2 - tY; // 这里用于计算广告可显示区域

        this.bannerAd = wx.createBannerAd({
            adUnitId: BannerUnitId,
            adIntervals: 60, // 自动刷新频率不能小于30秒
            style: {
                left: 0,
                top: th - tHeight,
                width: tw
            }
        })

        this.bannerAd.onLoad(() => {
            console.log('banner 广告加载成功')
        })
        this.bannerAd.onError(err => {
            console.log('banner 广告拉取失败')
            console.log(err)
        })

        this.bannerAd.onResize(res => {
            if (res.height > tHeight) {
                // 广告高度大于显示高度,此时需要缩小
                var scale = (tHeight / res.height);
                var tADWidth = res.width * scale;
                if (tADWidth < 300) {
                    // 缩小之后的宽度小于300，则表示显示区域小于广告区域，且不可调节,则应该使用上边沿对齐，下边会超出屏幕
                    var scale = 300 / res.width;
                    // var tADHeight = res.height * scale;
                    this.bannerAd.style.width = 300;
                    this.bannerAd.style.top = tY;
                } else {
                    // 缩小之后的宽度大于300，则表示显示区域大于广告,则应该使用下边沿对齐
                    this.bannerAd.style.width = tADWidth;
                    this.bannerAd.style.top = th - tHeight;
                }
            } else {
                // 广告高度小于显示高度,此时需要放大

                // 先放大高度，如果放大后宽度小于屏幕宽度
                var scale = (tHeight / res.height);
                if (res.width * scale <= tw) {
                    this.bannerAd.style.width = res.width * scale;
                    this.bannerAd.style.top = th - tHeight;
                } else {
                    var scale = (tw / res.width);
                    this.bannerAd.style.height = tw;
                    this.bannerAd.style.top = th - res.height * scale;
                }
            }

            this.bannerAd.style.left = (tw - this.bannerAd.style.width) / 2;
        })
    },

    showBannerAd() {
        this.bannerAd.show()
            .then(() => console.log('banner 广告显示成功'))
            .catch(err => {
                console.log('banner 广告显示错误')
                console.log(err);
            })
    },

    hideBannerAd() {
        this.bannerAd.hide();
    },

    initVideoAd() {
        if (this.videoAd) {
            return;
        }

        this.videoAd = wx.createRewardedVideoAd({ adUnitId: VideoUnitId });

        this.videoAd.onLoad(() => {
            console.log('激励视频 广告加载成功')
        })

        this.videoAd.onError(err => {
            console.log('激励视频 广告拉取失败')
            console.log(err)
        })

        this.videoAd.onClose(res => {
            // 用户点击了【关闭广告】按钮
            // 小于 2.1.0 的基础库版本，res 是一个 undefined
            if (res && res.isEnded || res === undefined) {
                // 正常播放结束，可以下发游戏奖励
                this.videoAdCb && this.videoAdCb({ event: this.EVENT_SHOW_SUCC, param: {} });
            }
            else {
                // 播放中途退出，不下发游戏奖励
                this.videoAdCb && this.videoAdCb({ event: this.EVENT_SHOW_FAIL, param: {} });
            }
            this.videoAdCb = null;
        })
    },

    showVideoAd() {
        this.videoAd.show()
            .then(() => console.log('激励视频 广告显示成功'))
            .catch(err => {
                console.log('激励视频 广告显示错误')
                console.log(err);
                this.videoAd.load()
                    .then(() => this.videoAd.show())
            })
    },


    initInterstitialAd() {
        this.interstitialAd = wx.createInterstitialAd({ adUnitId: InterstitialUnitId });
        this.interstitialAd.onLoad(() => {
            console.log('插屏 广告加载成功')
        });
        this.interstitialAd.onError(err => {
            console.log('插屏 广告拉取失败')
            console.log(err)
        });

        this.interstitialAd.onClose(res => {
            console.log('插屏 广告关闭')
            this.interstitialCb && this.interstitialCb({ event: this.EVENT_SHOW_SUCC, param: {} });
            this.interstitialCb = null;
        })
    },

    showInterstitialAd() {
        this.interstitialAd.show()
            .then(() => console.log('插屏 广告显示成功'))
            .catch((err) => {
                console.log('激励视频 广告显示错误')
                console.log(err);
                this.interstitialAd.load()
                    .then(() => this.interstitialAd.show())
            })
    },

    show(adType, cb) {
        if (adType == ADMgr.AD_BANNER) {
            this.showBannerAd();
        } else if (adType == ADMgr.AD_VIDEO) {
            this.videoAdCb = cb;
            this.showVideoAd();
        } else if (adType == ADMgr.AD_INTERSTITIAL) {
            this.interstitialCb = cb;
            this.showInterstitialAd();
        }
    },

    hide(adType) {
        if (adType == ADMgr.AD_BANNER) {
            this.hideBannerAd();
        }
    },
}